<template>
    <div class='dialog'>
        <div class='newAdd dialog-con channelinfomation'>
          <split-vertical title="新增商品渠道信息"></split-vertical>
            <div class="channelAD">
                <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm" inline :label-position="labelPosition">
                  <el-form-item label="渠道名称" prop="channelId">
                     <el-select v-model="ruleForm.channelId" placeholder="请选择" @change='getChannel(ruleForm.channelId)'>
                        <el-option
                          v-for="(item,index) in channelInfo"
                          :key="index"
                          :label="item.channelName"
                          :value="item.id">
                        </el-option>
                      </el-select>
                  </el-form-item>
                  <el-form-item label="授权产品ID" prop="productId"  >
                    <el-input v-model="ruleForm.productId" style="width:120px;"></el-input>
                    <el-button @click="findAuthInfo" size="medium">查询</el-button>
                  </el-form-item>
                  <el-form-item label="联系人" prop="channelContact">
                    <el-input v-model="channelContact" disabled></el-input>
                  </el-form-item>
                  <el-form-item label="供应商进价" prop="purchasePrice"  >
                    <el-input v-model="shopInfomation.purchasePrice" disabled></el-input>
                  </el-form-item>
                  <el-form-item label="联系电话" prop="channelMobile" disabled>
                    <el-input v-model="channelMobile" disabled></el-input>
                  </el-form-item>
                  <el-form-item label="商品sku" prop="sku">
                    <el-input v-model="shopInfomation.itemSkuName" disabled></el-input>
                  </el-form-item>
                   <el-form-item label="渠道地址" prop="channelAddress">
                    <el-input v-model="channelAddress" disabled></el-input>
                  </el-form-item>
                  <el-form-item label="渠道售价(￥)" prop="channelPrice">
                    <el-input v-model="ruleForm.channelPrice"></el-input>
                  </el-form-item>
                  <el-form-item label="渠道商品编号" prop="channelProductNo" >
                    <el-input v-model.trim="ruleForm.channelProductNo"></el-input>
                  </el-form-item>
                  <el-form-item label="渠道商品名" prop="channelProductName">
                    <el-input v-model="ruleForm.channelProductName"></el-input>
                  </el-form-item>
                  <el-checkbox v-model="checked" @change="isInput" style="display:block">可选项</el-checkbox>
                  <el-form-item label="金额佣金(%)" prop="commission">
                    <el-input v-model="ruleForm.commission" @change="commssionVal(ruleForm.commission)" :disabled="disabled"></el-input>
                  </el-form-item>
                  <el-form-item label="分期佣金(%)" prop="staging">
                    <el-input v-model="ruleForm.staging" @change="stagingVal(ruleForm.staging)" :disabled="disabled"></el-input>
                  </el-form-item>
                  <el-form-item label="积分佣金(%)" prop="integral">
                    <el-input v-model="ruleForm.integral" @change="integralVal(ruleForm.integral)" :disabled="disabled"></el-input>
                  </el-form-item>
                  <el-form-item class="createChannelINfo">
                    <el-button @click="submitForm('ruleForm')" type="primary" :disabled="isDisabled" size="medium">创建</el-button>
                    <el-button @click="cancel"  size="medium">取消</el-button>
                  </el-form-item>
              </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import regular from 'js/regular'
import {localStorage} from 'js/localStorage'
import SplitVertical from "components/common/SplitVertical"
export default {
  inject: ["reload"],
  components:{
    SplitVertical
  },
  data() {
    return {
      //创建按钮是否显示
      isDisabled:false,
      //复选框
      checked: false,
      disabled:true,
      //渠道名称列表
      channelInfo:[],
      shopInfomation:{},
      labelPosition:'left',
      channelAddress:'',
      channelMobile:'',
      channelContact:'',
      ruleForm: {
        channelId:'',
        channelProductName:'',
        channelProductNo:'',
        commission:'',
        integral:'',
        staging:'',
        channelPrice:''
      },
      rules: {
        channelProductNo: [
          { required: true, message: '请输入渠道商品编号', trigger: 'change' }
        ],
        // integral:[
        //   { required: true, message: '请输入积分', trigger: 'change' }
        // ],
        // staging:[
        //   { required: true, message: '请输入分期佣金', trigger: 'change' }
        // ],
        // commission:[
        //   { required: true, message: '请输入全额佣金', trigger: 'change' }
        // ],
        channelPrice:[
          { required: true, message: '请输入渠道售价', trigger: 'change' }
        ],
        channelProductName:[
            { required: true, message: '请输入渠道商品名', trigger: 'change' }
        ],
        productId:[
          { required: true, message: '请输入授权产品ID', trigger: 'change' }
        ],
        channelId:[
          { required: true, message: '请选择渠道名称', trigger: 'change' }
        ]
      }
    };
  },
  created(){
    this.userId=localStorage.get('userID')
    this.init()
  },
  methods: {
     init(){
      //获取渠道详情
      this.getchannelInfomation()
    },
    //改变复选框
    isInput(val){
      if(val==true){
        this.disabled=false
      }else{
        this.disabled=true
      }
    },
    getchannelInfomation(){
      let success=data=>{
        if(data.code=="OK"){
          this.channelInfo=data.data
        }else{
          this.$message.error(data.msg)
        }
      }
      this.api.channelAll({},success)
    },
    //获取创建的内容
    findAuthInfo(){
      let parmas={
        id:this.ruleForm.productId
      }
      if(parmas){
          let success=data=>{
              if(data.code==='OK'){
                this.shopInfomation=data.data
              }else{
                this.$message.error(data.msg)
              }
          }
        this.api.getCreateChannelInfo(parmas,success)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
            this.confirm()
        } else {
            return false;
        }
      });
    },
    getChannel(val){
      for(var i in this.channelInfo){
        var id=this.channelInfo[i].id
        if(id==val){
            this.channelAddress=this.channelInfo[i].channelAddress ||null
            this.channelContact=this.channelInfo[i].channelContact ||null
            this.channelMobile=this.channelInfo[i].channelMobile ||null
        }
      }
    },
    confirm(){
        if(!regular.code(this.ruleForm.channelProductNo)){
          this.$message.warning('只能输入数字和大小写字母')
        }
        let parmas=this.ruleForm
        parmas['userId']=this.userId
        this.isDisabled=true
        let success = data => {
          if (data.code == "OK") {
            this.$message.success("添加成功");
            this.isDisabled = false;
            this.reload();
            this.$emit("closeDialog");
          } else {
            this.isDisabled = false;
            this.$message.error(data.msg);
          }
        };
      this.api.channelProductAdd(parmas, success);
    },
    cancel() {
      this.$emit("closeDialog");
    },
    commssionVal(val){
      if(!regular.Percentage(val)){
        this.$message.error('输入的数字不能100%')
      }
    },
    stagingVal(val){
      if(!regular.Percentage(val)){
        this.$message.error('输入的数字不能100%')
      }
    },
    integralVal(val){
      if(!regular.Percentage(val)){
        this.$message.error('输入的数字不能100%')
      }
    },
  }
};
</script>
<style scoped>
 .channelinfomation .el-select {
    width:200px;
  }
  .channelinfomation .el-input {
    width:200px;
  }
</style>
<style lang="scss" scoped>
.channelinfomation {
    width: 700px;
    height: 540px;
  .companynews {
    h2 {
      height: 60px;
      line-height: 60px;
      text-indent: 20px;
      font-size: 20px;
    }
  }
  .create{
    position: relative;
    left:22%;
    top:30%;
    margin-top: 46px;
  }
  .channelAD{
    margin-left:20px;
  }
  .createChannelINfo{
    position: relative;
    left:22%;
    margin-top: 20px;
  }
}
</style>

